#include "Linklist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>


void test_inst(char *str, llist_t *head, int randnum)
{
	sprintf(str, "this is str_%d\0", randnum);
	printf("<<<<INSERT %s >>>> result:return %ld\n",str,  (long)lklst_insert(head, (void *)str, strlen(str)));
}

void test_del(char *str, llist_t *head, int randnum)
{
	lnode_t *pos = NULL;

	sprintf(str, "this is str_%d\0", randnum);
//	while((pos = lklst_query(head, pos, str, strcmp)) != NULL)
	{
	//	printf("[[[[DELETE: %s ]]]] result:%ld\n",str, (long)lklst_delete(head, str, strcmp));
		printf("[[[[DELETE: %s ]]]] result:%ld\t[[[[DELETE_ALL]]]]\n",str, (long)lklst_delete_all(head, str, strcmp));

	}
}

void test_query(char *str, llist_t *head, int randnum)
{
	lnode_t *pos = NULL;
	sprintf(str, "this is str_%d\0", randnum);

	while((pos = lklst_query(head, pos, str, strcmp)) != NULL)
		printf("(((( QUERY %s )))) result:%ld, 结点内容: %s\n",str, (long)pos, (char *)lklst_get_content(head, pos, str));

}


int main()
{
	int	choice ;
	int randnum;
	llist_t		*head;
	lnode_t		*node;
	char *str = (char *)malloc(50);

	head = lklst_init();
	printf("head = %ld\n", (long)head);

	while(1)
	{
			srand(time(NULL));
			//choice = rand()%4;
			choice = rand()%3 +1;

			srand(time(NULL));
			randnum = rand()%10;
			switch(choice)
			{
				case 0:
					srand(time(NULL));
					if(rand()%7 == 0)
						printf("XXXX DESTORY XXXXXresult: %ld", (long)lklst_destroy( head));
					break;
				case 1:
					test_inst(str, head ,randnum);
					break;
				case 2:
					test_del(str, head, randnum);
					break;
				case 3:
					test_query(str, head, randnum);
					break;
			}
			printf("现在表内容:");
			node = head->first;
			while(node != NULL)
			{
				printf("%s\t", (char *)node->dataptr);
				node = node->next;
			}
			printf("\n\n");
	}
	return 0;
}
